package com.xzuf.finance;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.table.DefaultTableModel;

import org.apache.commons.beanutils.BeanUtils;

/**
 * @author xzgf
 * @since 2008-8-13
 * @version 1.0
 */
public class FinanceTableModel extends DefaultTableModel {
	
//	private static final Log logger = LogFactory.getLog(FinanceTableModel.class);
	
	private FinanceDataItem[] columnList;
	
	private Map<String, FinanceDataItem> htKeyItems = new HashMap<String, FinanceDataItem>();
	
	public FinanceTableModel() {
		super();
	}
	
	public void setDataItemList(FinanceDataItem[] dataList) {
		this.columnList = dataList;
		if(this.columnList != null) {
			for(int i=0;i<columnList.length;i++) {
				htKeyItems.put(columnList[i].getKey(), columnList[i]);
			}
		}
	}
	
	public FinanceDataItem[] getDataItem() {
		return columnList;
	}
	
	public void setBodyDataVO(FinanceDataVO[] vos) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		
		if(vos == null || vos.length <= 0) {
			return;
		}
		dataVector = new Vector();
		dataVector.ensureCapacity(vos.length);
		Object oValue = null;
		
		for(int i=0;i<vos.length;i++) {
			Vector rowV = new Vector();
			initRowVector(rowV);
			for(int j=0;j<columnList.length;j++) {
				oValue = BeanUtils.getSimpleProperty(vos[i], columnList[j].getKey());
				rowV.setElementAt(oValue, j);
			}
			dataVector.add(rowV);
		}
		
		fireTableDataChanged();
	}
	
	private void initRowVector(Vector v) {
		for(int i=0;i<columnList.length;i++) {
			v.add("");
		}
	}
	
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		
		if(columnIndex == 9) {
			return Number.class;
		}
		return columnList[columnIndex].getClass();
	}

	@Override
	public int getColumnCount() {
		if(columnList != null) 
			return columnList.length;
		return 0;
	}
	
	@Override
	public String getColumnName(int column) {
		if(columnList != null) {
			return columnList[column].getValue();
		}
		return super.getColumnName(column);
	}
	
	@Override
	public Object getValueAt(int row, int column) {
		Vector rowV = (Vector)dataVector.get(row);
		return rowV.get(column);
	}
}
